package com.itheima.wms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pinda.base.BaseController;
import com.itheima.pinda.base.R;
import com.itheima.pinda.utils.SpringUtils;
import com.itheima.wms.dto.LocationDTO;
import com.itheima.wms.entity.LocationEntity;
import com.itheima.wms.excel.service.LocationExcelService;
import com.itheima.wms.excel.service.impl.LocationExcelServiceImpl;
import com.itheima.wms.service.LocationService;
import com.itheima.wms.vo.ImportResultVO;
import com.itheima.wms.vo.LocationDetailVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

import static com.itheima.pinda.exception.code.ExceptionCode.BASE_VALID_PARAM;

@Slf4j
@ApiOperation(value = "Location", tags = "库位")
@RestController
@RequestMapping("/location")
public class LocationController extends BaseController {

    @Autowired
    private LocationService locationService;

    @Autowired
    private LocationExcelService locationExcelService;

    /**
     * 更新操作
     * @param locationDTO
     * @return
     */
    @PutMapping
    public R<LocationEntity> update(@RequestBody LocationDTO locationDTO){
        LocationEntity locationEntity = locationService.update(locationDTO);
        return R.success(locationEntity);
    }

    /**
     * 分页查询
     * @param map
     * @return
     */
    @GetMapping("/page")
    public R<IPage<LocationEntity>> page(@RequestParam Map map){
        Page<LocationEntity> page = getPage();
        IPage<LocationEntity> locationPage = locationService.page(page, map);
        page.setRecords(locationPage.getRecords());
        return R.success(page);
    }

    /**
     * 分页查询库位详细信息
     */
    @GetMapping("/pageDetail")
    public R<IPage<LocationDetailVO>> pageDetail(@RequestParam Map map){
        Page<LocationDetailVO> page = getPage();
        IPage<LocationDetailVO> areaPage = locationService.pageDetail(page,map);
        page.setRecords(areaPage.getRecords());
        return R.success(page);
    }

    /**
     * 查询全部的库位
     */
    @GetMapping("/list")
    public R<List<LocationEntity>> list(@RequestParam Map data){
        List<LocationEntity> locationArray = locationService.list(data);
        return R.success(locationArray);
    }

    /**
     * 查询库位详情
     */
    @GetMapping("/listDetail")
    public R<List<LocationDetailVO>> listDetail(@RequestParam Map map){
        List<LocationDetailVO> locationArray = locationService.listDetail(map);
        return R.success(locationArray);
    }

    /**
     * 查询库位
     */
    @GetMapping("/{id}")
    public R<LocationEntity> get(@PathVariable Long id){
        return R.success(locationService.getById(id));
    }

    /**
     * 新增库位
     */
    @PostMapping
    public R<LocationEntity> save(@RequestBody LocationDTO data){
        locationService.save(data);
        return R.success(data);
    }

    /**
     * 删除库位
     *
     * @param ids 主键id
     * @return 删除结果
     */
    @DeleteMapping
    public R<Boolean> delete(@RequestParam("ids[]") List<Long> ids) {
        locationService.removeByIds(ids);
        return success();
    }

    /**
     * 批量导入
     * @param file
     * @return
     */
    @PostMapping("upload")
    public R<? extends Object> upload(@RequestParam(value = "file") MultipartFile file) {
        if (file.isEmpty()) {
            return fail(BASE_VALID_PARAM.build("导入内容为空"));
        }
        Long begin = System.currentTimeMillis();
//        ImportResultVO importResultVO = locationService.importExcel(file);

        // 获取新对象
        locationExcelService = SpringUtils.getBean(LocationExcelServiceImpl.class);
        ImportResultVO importResultVO = locationExcelService.analysis(file);
        Long end = System.currentTimeMillis();

        log.info("导入excel 用时 :{}", (end - begin));
        return R.success(importResultVO);
    }
}
